package com.khh.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by webrx on 2017-09-05.
 */
@Controller
public class UpFeil {


    //获取文件信息
    /*@RequestMapping("/upload") @ResponseBody
    public void upfile(@RequestParam("ff") CommonsMultipartFile cmf, String name, PrintWriter out) throws IOException {
        System.out.println(name);
        String filename = cmf.getOriginalFilename();//获取文件元名
        cmf.transferTo(new File("c:/",filename));
        out.print(String.format("文件名:%s字节<br>",filename));
        out.print(String.format("表单名:%s字节<br>",cmf.getName()));
        out.print(String.format("文件大小写:%d字节<br>",cmf.getSize()));
        //out.print(cmf.getContentType());
    }*/

    @ResponseBody
    @RequestMapping("/upload.do")
    public void upload(@RequestParam("ff") CommonsMultipartFile f, HttpServletResponse resp, HttpServletRequest req) throws IOException {


        PrintWriter out = resp.getWriter();
        out.print(f.getName() + "<br>"); //表单名 face
        out.print(f.getSize() + "<br>");//上传的大小字节
        out.print(f.getOriginalFilename() + "<br>");//01mybatis简介与入门.txt
        out.print(f.getContentType() + "<br>");
        out.flush();
        out.close();
    }


    //单文件上传
    @RequestMapping("/uploads.do")
    public ModelAndView upfile(@RequestParam("face") CommonsMultipartFile cmf, HttpServletRequest req) throws IOException {
        ModelAndView mv = new ModelAndView("upload/upone");  //WEB-INF/views/upf.jsp
        if (cmf.isEmpty()) {
            mv.addObject("info", "文件上传失败，没有选择文件");
            //mv.setViewName("upf-error.jsp");
        } else {
            //建立目录

            String s = new SimpleDateFormat("yyMMdd").format(new Date());  //1709   /upload/1709/
            String path = "/upload/" + s;
            String p = req.getServletContext().getRealPath(path);
            File ff = new File(p);
            if (!ff.exists()) {
                ff.mkdirs();
            }

            String nn = cmf.getOriginalFilename();

            cmf.transferTo(new File(p, nn));

            mv.addObject("name", cmf.getOriginalFilename());
            mv.addObject("size", cmf.getSize());
            mv.addObject("contenttype", cmf.getContentType());
            mv.addObject("formname", cmf.getName());
            //mv.setViewName("upf-success.jsp");
        }
        return mv;
    }


    //多文件上传
    @RequestMapping("/uploads")
    public ModelAndView ups(@RequestParam("head") CommonsMultipartFile cmf[], HttpServletRequest req) throws IOException {
        ModelAndView mv = new ModelAndView("upload/upmany");

        //上传文件路径
        String s = new SimpleDateFormat("yyMMdd").format(new Date());  //1709   /upload/1709/
        String path = "/upload/" + s;
        String p = req.getServletContext().getRealPath(path);
        File ff = new File(p);
        if (!ff.exists()) {
            ff.mkdirs();
        }

        for(CommonsMultipartFile f : cmf){
            //源文件名
            String name = f.getOriginalFilename();
            //扩展名
            String ext = name.substring(name.lastIndexOf(".")).toLowerCase();
            //uuid命名 或时间命名
            //略
            //文件大小
            long size = f.getSize();
            System.out.println(size);
            if(size<1024*100){
                if (ext.equalsIgnoreCase(".jpg") || ext.equalsIgnoreCase(".png")) {
                    f.transferTo(new File(p,name));
                } else {
                mv.addObject("info", "文件类型不对，只允许JPG，png格式");
                }
            } else {
                mv.addObject("info", "文件过大，需要小于100k");
                mv.addObject("infoname", "失败文件名"+name);
            }
        }
        return mv;
    }


    //多文件上传
    @RequestMapping("/uploads.action")
    public ModelAndView ups2(@RequestParam("fs") CommonsMultipartFile cmf[], HttpServletRequest req) throws IOException {
        ModelAndView mv = new ModelAndView("upload/upmany");

        //上传文件路径
        String s = new SimpleDateFormat("yyMMdd").format(new Date());  //1709   /upload/1709/
        String path = "/upload/" + s;
        String p = req.getServletContext().getRealPath(path);
        File ff = new File(p);
        if (!ff.exists()) {
            ff.mkdirs();
        }

        for(CommonsMultipartFile f : cmf){
            if(f.isEmpty()) continue;
            f.transferTo(new File(p,f.getOriginalFilename()));
        }
        return mv;
    }

}